package com.tuanzi.loan.web.bean.family;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.service.family.FeedbackService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.family.FeedbackSearchRequest;
import com.tuanzi.loan.vo.response.family.FeedbackSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.family.FeedbackLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.FEEDBACK_TYPE;

@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class FeedbackListBean extends JSFListingBean<FeedbackSearchRequest, FeedbackSearchResponse> {

	@Autowired
	private FeedbackService service;

	@PostConstruct
	private void init() {
		initLazyDataModel();
	}

	private void initLazyDataModel() {
		$ = new FeedbackSearchRequest();
		lazyDataModel = new FeedbackLazyDataModel($, service);
	}

	public void onResolve(Integer id){
		if (id == null) {
			addErrorMessage("系统错误，请联系管理员");
		}
		service.resolve(id);
		addSuccessfullyMessage();
	}

	public void onDelete(Integer id){
		if (id == null) {
			addErrorMessage("系统错误，请联系管理员");
		}
		service.delete(id);
		addSuccessfullyMessage();
	}

	public void onReset() {
		initLazyDataModel();
	}

	@Override
	protected List<String> getDictionaryCodes() {
		return Lists.newArrayList(FEEDBACK_TYPE);
	}

	public String translateResolved(Boolean isResolved) {
		if (isResolved == null) {
			return null;
		}
		return isResolved ? "是" : "否";
	}
}
